<?php 
/**
* 
*/
class Employee_Model extends CI_Model
{
	
	function __construct()
	{
		parent::__construct();
	}

	public function checkUser($username){
		return $this->db->where('emp_user',$username)->get('Employee');
	}

	public function checkPasswordOfID($password,$id){
		return $this->db->where(array('id' => $id , 'emp_pass' => md5($password) )) ->get('Employee');
	}

	public function changeStatus($id,$status){
		$this->db->where('id',$id)->update('Employee',array('emp_status'=> $status));
	}

	public function changePassword($id,$newPass){
		$this->db->where('id', $id)->update('Employee',array('emp_pass' => md5($newPass) ));
	}

	public function updateFailcount($id,$failcount){
		$this->db->where('id', $id)->update('Employee',array('login_failcount' => $failcount ));
	}

	public function clearFailcount($id){
		$this->db->where('id',$id)->update('Employee',array('login_failcount' => 0));
	}

	public function getLastEmployeeID(){
		$rs = $this->db->query('SELECT TOP 1 id FROM Employee ORDER BY id DESC')->row_array();
		return $rs['id'];
	}

	public function getUser($id){
		return $this->db->get_where('Employee',array('id'=>$id));
	}

	public function checkNewPasswordWithOldPassword($newPass,$id){
		return $this->db->get_where('Employee',array('id'=>$id ,'emp_pass'=>$newPass));
	}

	public function getAllEmployee(){
		return $this->db->query("select Employee.id, Employee.emp_user,title.ref_name+emp_name+' '+emp_lastname As emp_names,position.ref_name AS position,Department.ref_name As department,Supdepartment.ref_name As subdepartment,Employee.emp_status from Employee 
			LEFT OUTER JOIN Mst_reference as title ON Employee.emp_title = title.ref_code and title.ref_group = 'title'
			LEFT OUTER JOIN Mst_reference as position ON Employee.emp_position_code = position.ref_code and position.ref_group = 'position'
			LEFT OUTER JOIN Mst_reference AS Department ON Employee.emp_department_code = Department.ref_code and Department.ref_group = 'department'
			LEFT OUTER JOIN Mst_reference AS Supdepartment ON Employee.emp_subdepartment_code = Supdepartment.ref_code and Supdepartment.ref_group = 'sub_department'
		")->result_array();
	}

	public function insert($newEmp){
		$this->db->insert('Employee',$newEmp);
	}

	public function update($id,$newData){
		$this->db->where('id',$id)->update('Employee',$newData);
	}

	public function delete(){

	}

	public function getUserInfo($id){
		$rs = $this->db->query("SELECT e.id ,title.ref_name AS title ,e.emp_name ,e.emp_lastname , r.role_name AS role ,position.ref_name AS position ,department.ref_name AS department , subdepartment.ref_name AS subdepartment ,e.amcam_user ,e.amcats_user ,e.emp_tel, e.emp_user ,e.emp_status ,e.login_failcount
FROM Employee e
INNER JOIN Mst_reference as title ON e.emp_title = title.ref_code AND title.ref_group = 'title'
INNER JOIN Mst_reference as position ON e.emp_position_code = position.ref_code AND position.ref_group = 'position'
INNER JOIN Mst_reference as department ON e.emp_department_code = department.ref_code AND department.ref_group = 'department'
INNER JOIN Mst_reference as subdepartment ON e.emp_subdepartment_code = subdepartment.ref_code AND subdepartment.ref_group = 'sub_department'
INNER JOIN Role r ON e.emp_role = r.role_id  WHERE e.id = '".$id."'")->row_array();

		return $rs;
	}

	public function getPermissionUser($id){
		return $this->db->query("SELECT p.per_name ,rp.per_view ,rp.per_add ,rp.per_update ,rp.per_delete ,rp.per_approve ,rp.per_export FROM Employee e 
			INNER JOIN Role_Permission rp ON e.emp_role = rp.role_id 
			INNER JOIN Permission p ON rp.per_id = p.per_id
			WHERE e.id = '".$id."'")->result_array();
	}

	

}
?>